package cn.sunshine.system.controller;
import cn.sunshine.basic.utils.AjaxResult;
import cn.sunshine.basic.utils.PageList;
import cn.sunshine.system.domain.DictionaryDetail;
import cn.sunshine.system.query.DictionaryDetailQuery;
import cn.sunshine.system.service.IDictionaryDetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;


/*
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:42
     * @Parameters: [id]
     * @Return cn.sunshine.org.domain.Employee
     */


    @GetMapping("/{id}")
    public DictionaryDetail loadById(@PathVariable("id") Long id){

        return dictionaryDetailService.loadById(id);
    }
    @GetMapping
    public List<DictionaryDetail> loadAll(){

        return dictionaryDetailService.loadAll();
    }





/*
     * @Title: queryData
     * @Description: 高级查询查询所有
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:43
     * @Parameters: [employeeQuery]
     * @Return cn.sunshine.basic.utils.PageList<cn.sunshine.org.domain.Employee>
     */


    @PostMapping
    public PageList<DictionaryDetail> queryData(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        System.out.println(dictionaryDetailQuery);
        return dictionaryDetailService.queryData(dictionaryDetailQuery);
    }

/*
     * @Title: remove
     * @Description: 根据id删除数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:58
     * @Parameters: [id]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */


    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.remove(id);
            return AjaxResult.getAjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

/*
     * @Title: addOrUpdate
     * @Description: 增加与修改
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 18:17
     * @Parameters: [employee]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */


    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);

            }else {
                dictionaryDetailService.update(dictionaryDetail);

            }
            return AjaxResult.getAjaxResult();

        }catch (RuntimeException e){
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }


/*
     * @Title: batchEmployee
     * @Description: 批量删除部门数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/25 0:25
     * @Parameters: [ids]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */


@PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {// 调用删除方法，根据id删除
            dictionaryDetailService.batchRemove( ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");

        }
    }

    @GetMapping("/type/{id}")

    public List<DictionaryDetail> loadType(@PathVariable Long id){
        return dictionaryDetailService.loadType(id);
    }

}

